<?php


namespace app\api\controller;

use app\api\model\Wallet as WalletModel;
use app\api\model\User as Usermodel;
/***
 * 用户钱包控制器
 * Class Wallet
 * @package app\api\controller
 */
class Wallet extends Base
{
    //初始余额
    private $init_money = 0.00;

    /***
     * Desc :创建钱包
     * User : HAIYUN
     * Date : 2022-01-06 15:08
     * @param $init_money
     * @param $uid
     */
    public function createWallet($uid)
    {
        //查询用户是否存在
        //根据uid查找用户数据，判断是否存在
        $userModel = new Usermodel();
        $user = $userModel->where("id",$uid)->find();
        if(!$user){
            $this->return_msg("20004","用户不存在",[]);
        }
        //查看该用户是否有钱包
        $walletModel = new WalletModel();
        $wallet = $walletModel->where('user_id',$uid)->find();
        if($wallet){
            $this->return_msg("20005","该用户钱包已存在",[]);
        }
        //创建钱包
        $info = [
            "user_id" => $uid,
            "money" => $this->init_money,
        ];
        $wallet_info = $walletModel::create($info);
        if(!$wallet_info){
            $this->return_msg("20001","钱包创建失败",[]);
        }
    }

    /***
     * Desc :通用充值提现
     * User : HAIYUN
     * Date : 2022-01-06 16:52
     */
    private function updateWallet($params,$type)
    {
        //查询用户是否存在
        //根据token获取uid
        $uid = $this->getCurrentTokenVar("uid");
        //根据uid查找用户数据，判断是否存在
        $userModel = new Usermodel();
        $user = $userModel->where("id",$uid)->find();
        if(!$user){
            $this->return_msg("20004","用户不存在",[]);
        }
        //查看该用户是否有钱包
        $walletModel = new WalletModel();
        $wallet = $walletModel->where('user_id',$uid)->find();
        if(!$wallet){
            $this->return_msg("20004","未查询到该用户钱包信息",[]);
        }
        //校验参数
        $rules = [
            ['money', 'require|>:0.00', '10001|10002'],
        ];
        $msg   = $this->validate($params, $rules);
        if ($msg !== true) $this->return_msg($msg, '参数错误');
        //根据类型来确定取款存款
        switch ($type){
            case "recharge" :
                //新余额等于充值+原来余额
                $balance = $wallet['money'] + $this->params['money'];
                $msg = "充值";
                break;
            case "withdraw" :
                //新余额等于充值-原来余额
                $balance = $wallet['money'] - $this->params['money'];
                $msg = "提现";
                //取款不能超过余额
                if($balance < 0){
                    $this->return_msg("10002","提现金额超过余额",[]);
                }
                break;
            default:
                $this->return_msg("10000","未知的操作类型",[]);
                break;
        }
        //过滤不必要的参数
        $safe_data = $this->getDataByRule($this->params, $rules);
        $safe_data['money'] = $balance;
        //修改钱包余额
        $res = $walletModel->edit($safe_data,['user_id'=>$uid]);
        if ($res !== false) {
            $this->return_msg("00000", $msg."成功", $safe_data);
        } else {
            $this->return_msg("20002", $msg."失败", []);
        }
    }

    /***
     * Desc :钱包充值
     * User : HAIYUN
     * Date : 2022-01-06 16:27
     */
    public function recharge()
    {
        $this->updateWallet($this->params,"recharge");
    }

    /***
     * Desc :钱包提现
     * User : HAIYUN
     * Date : 2022-01-06 17:01
     */
    public function withdraw()
    {
        $this->updateWallet($this->params,"withdraw");
    }

    /***
     * Desc :查看钱包
     * User : HAIYUN
     * Date : 2022-01-06 17:05
     */
    public function myWallet()
    {
        //查询用户是否存在
        //根据token获取uid
        $uid = $this->getCurrentTokenVar("uid");
        //根据uid查找用户数据，判断是否存在
        $userModel = new Usermodel();
        $user = $userModel->where("id",$uid)->find();
        if(!$user){
            $this->return_msg("20004","用户不存在",[]);
        }
        //查看该用户是否有钱包
        $walletModel = new WalletModel();
        $wallet = $walletModel->where('user_id',$uid)->find();
        if(!$wallet){
            $this->return_msg("20004","未查询到该用户钱包信息",[]);
        }
        $this->return_msg("00000","查询钱包余额成功",$wallet['money']);
    }

}